/**
 * @author LKQ
 * @date 2022/1/30 15:13
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.shortestToChar("loveleetcode", 'e');
    }
    public int[] shortestToChar(String s, char c) {
        int n = s.length();
        int[] ans = new int[n];
        char[] arr = s.toCharArray();
        for (int i = 0; i < n; i++) {
            // 如果当前字符等于c，距离为0
            if (arr[i] == c) {
                ans[i] = 0;
                continue;
            }
            int left = i - 1, right = i + 1;
            while (left >= 0 && arr[left] != c) {
                left--;
            }
            while (right < n && arr[right] != c) {
                right++;
            }
            if ( left < 0) {
                // 左边超出边界没找到
                ans[i] = Math.abs(right - i);
            }else if (right >= n) {
                // 右边超出边界也没找到
                ans[i] = Math.abs(left - i);
            }else {
                ans[i] = Math.min(Math.abs(left - i), Math.abs(right - i));
            }
        }
        return ans;
    }
}
